package com.dwxt.sport.admin.service;

import com.dwxt.sport.dao.UserMapper;
import com.dwxt.sport.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserServiceAdmin {

    @Autowired
    private UserMapper userMapper;

    public User login(String phone, String password) {
        User userByPhone = selectUserByPhone(phone);
        if (userByPhone != null && userByPhone.getPassword().equalsIgnoreCase(password)) {
            return userByPhone;
        } else {
            return null;
        }
    }

    /**
     * 由电话号码查询用户
     *
     * @param phone
     * @return
     */
    public User selectUserByPhone(String phone) {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("phone", phone);
        List<User> users = userMapper.selectByExample(example);
        return users.size() > 0 ? users.get(0) : null;
    }
}
